package com.rongshu.view.penpal.widget;

import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.penpal.bean.MemberDetails;
import com.rongshu.view.penpal.view.PenpalMemberManagerListener;

/**
 * Created by zhengweis on 2018/12/12.
 */

public class MemberOptionView extends LinearLayout implements View.OnClickListener {
    private TextView remove, member, viceLeader, leader, letter;
    private int RANK_LEADER = 1;//会长
    private int RANK_VIEW_LEADER = 2;//副会长
    private PenpalMemberManagerListener listener;
    private MemberDetails details;

    public MemberOptionView(Context context) {
        this(context, null, 0);
    }

    public MemberOptionView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MemberOptionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MemberOptionView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    public void setClickListener(PenpalMemberManagerListener listener, MemberDetails details) {
        this.listener = listener;
        this.details = details;
    }

    private void initView() {
        setOrientation(LinearLayout.HORIZONTAL);
        View view = View.inflate(getContext(), R.layout.adapter_penpal_member_bottom, this);
        remove = view.findViewById(R.id.remove_penpal);
        member = view.findViewById(R.id.to_member);
        viceLeader = view.findViewById(R.id.to_vice_leader);
        leader = view.findViewById(R.id.to_leader);
        letter = view.findViewById(R.id.private_letter);
        remove.setOnClickListener(this);
        member.setOnClickListener(this);
        viceLeader.setOnClickListener(this);
        leader.setOnClickListener(this);
        letter.setOnClickListener(this);
    }

    public void showViewByRank(int currentRank, int memberRank) {
        if (currentRank == RANK_LEADER) {
            if (memberRank == RANK_VIEW_LEADER) {
                remove.setVisibility(View.VISIBLE);
                member.setVisibility(View.VISIBLE);
                leader.setVisibility(View.VISIBLE);
                letter.setVisibility(View.VISIBLE);
                viceLeader.setVisibility(View.GONE);
            } else {
                remove.setVisibility(View.VISIBLE);
                member.setVisibility(View.GONE);
                leader.setVisibility(View.GONE);
                letter.setVisibility(View.VISIBLE);
                viceLeader.setVisibility(View.VISIBLE);
            }
        } else if (currentRank == RANK_VIEW_LEADER) {
            if (memberRank == RANK_VIEW_LEADER) {
                remove.setVisibility(View.GONE);
                member.setVisibility(View.GONE);
                leader.setVisibility(View.GONE);
                letter.setVisibility(View.VISIBLE);
                viceLeader.setVisibility(View.GONE);
            } else {
                remove.setVisibility(View.VISIBLE);
                member.setVisibility(View.GONE);
                leader.setVisibility(View.GONE);
                letter.setVisibility(View.VISIBLE);
                viceLeader.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onClick(View view) {
        if (listener == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.remove_penpal:
                listener.removePenpal(details);
                break;
            case R.id.to_member:
                listener.toMember(details);
                break;
            case R.id.to_vice_leader:
                listener.toViceLeader(details);
                break;
            case R.id.to_leader:
                listener.toLeader(details);
                break;
            case R.id.private_letter:
                listener.privateLetter(details);
                break;
        }
    }
}
